package personal.study.bl;

import java.sql.Connection;
import java.util.Collection;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import personal.study.db.DBStorage;
import personal.study.form.StorageForm;
import personal.study.util.DBConnection;

public class BLStorageAction {
	private static Log log = LogFactory.getLog(BLStorageAction.class);
	
	public Collection<StorageForm> queryStorage(String conditions, int currentPage, int rowsPerPage) throws Exception{
		Collection<StorageForm> col = null;
		Connection con = null;
		try{
			con = DBConnection.getCon();
			DBStorage dbStorage = new DBStorage();
			col = dbStorage.queryStorage(con, conditions, currentPage, rowsPerPage);
		}catch(Exception e){
			log.error(e.getMessage());
		}
		
		return col;
	}
	
	public Collection<StorageForm> queryStorage(String conditions) throws Exception{
		Collection<StorageForm> col = null;
		Connection con = null;
		try{
			con = DBConnection.getCon();
			DBStorage dbStorage = new DBStorage();
			col = dbStorage.queryStorage(con, conditions);
		}catch(Exception e){
			log.error(e.getMessage());
		}
		
		return col;
	}
	
	public int getCount(String conditions) throws Exception{
		int count = 0;
		Connection con = null;
		try{
			con = DBConnection.getCon();
			count = new DBStorage().getCount(con, conditions);
		}catch(Exception e){
			log.error(e.getMessage());
		}
		return count;
	}
	
	public boolean insert(StorageForm storageForm) throws Exception{
		boolean result = false;
		Connection con = null;
		try{
			con = DBConnection.getCon();
			DBStorage dbStorage = new DBStorage();
			result = dbStorage.insert(con, storageForm);
		}catch(Exception e){
			log.error(e.getMessage());
			result = false;
		}
		return result;
	}
	
	public StorageForm findByPrimaryKey(String productCode) throws Exception{
		StorageForm storageForm = null;
		Connection con = null;
		try{
			con = DBConnection.getCon();
			DBStorage dbStorage = new DBStorage();
			storageForm = dbStorage.findByPrimaryKey(con, productCode);
		}catch(Exception e){
			log.error(e.getMessage());
		}
		return storageForm;
	}
	
	public boolean update(StorageForm storageForm) throws Exception {
		boolean result = false;
		Connection con = null;
		try{
			con = DBConnection.getCon();
			DBStorage dbStorage = new DBStorage();
			result = dbStorage.update(con, storageForm);
		}catch(Exception e){
			log.error(e.getMessage());
			result = false;
		}
		
		return result;
	}
}
